package com.wnhz.work.jy.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.wnhz.work.jy.BaseActivity;
import com.wnhz.work.jy.MyApplication;
import com.wnhz.work.jy.R;
import com.wnhz.work.jy.bean.AssetsBean;
import com.wnhz.work.jy.utils.ConfigUtil;
import com.wnhz.work.jy.utils.xutils3.MyCallBack;
import com.wnhz.work.jy.utils.xutils3.XUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * 卖金币
 */
public class SaleGoldenActivity extends BaseActivity implements View.OnClickListener {
    private static final int SET_BANK_REQUESTCODE = 50;
    private static final int PAY_PASSWORK = 841;
    private TextView title;
    private TextView sale_all_money_tv;//出售总价
    private TextView consume_tv;//手续费
    private TextView account_money_tv;//最终所得
    private TextView sale_number_tv;//出售数量
    private TextView gold_number_tv;//拥有数量
    private TextView show_number_tv;//可卖数量
    private TextView one_quality_tv;//单枚质量
    private TextView year_tv;//年份
    private TextView quality_tv;//克数
    private TextView gold_type_tv;//金币种类
    private AssetsBean.MyGoods myGoods;
    private double rate;
    private final int SELECT_BANK = 813;
    private String bank_mame;
    private String bank_card_number;
    private TextView bank_mame_tv;
    private TextView bank_card_number_tv;
    private TextView bank_user_name_tv;
    private View bank_right_img;
    private View ts;
    private String id;
    private String name = "";
    private static final String TAG = "SaleGoldenActivity";

    private int num;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sale_golden);
        initTirle();

        myGoods = (AssetsBean.MyGoods) getIntent().getSerializableExtra("MyGoods");
        rate = Double.parseDouble(myGoods.getBack_rate());
        initView();

        initClick();
        ts = findViewById(R.id.ts);
        gold_number_tv.setText(myGoods.getShowNumber());
        show_number_tv.setText(myGoods.getGoodNumber());
        gold_type_tv.setText(myGoods.getGoodName());
        quality_tv.setText(myGoods.getWeight());
        year_tv.setText(myGoods.getAge());
        String s;
        if ("0".equals(myGoods.getIsAppreciation())){
            s = myGoods.getGoodPrice()+"元（涨幅+"+myGoods.getVariation()+"元)";
            one_quality_tv.setText(setColotSpannableString(0xffeda906,s));
        }else {
            s = myGoods.getGoodPrice()+"元（涨幅-"+myGoods.getVariation()+"元)";
            one_quality_tv.setText(setColotSpannableString(0xffff3c3d,s));
        }
        isshowbank();
    }

    private void initClick() {
        findViewById(R.id.sale_number_lay).setOnClickListener(this);
        findViewById(R.id.btn).setOnClickListener(this);
        findViewById(R.id.select_bank_card).setOnClickListener(this);
    }

    private void initView() {
        sale_all_money_tv = (TextView) findViewById(R.id.sale_all_money_tv);
        consume_tv = (TextView) findViewById(R.id.consume);
        account_money_tv = (TextView) findViewById(R.id.account_money_tv);
        sale_number_tv = (TextView) findViewById(R.id.sale_number_tv);
        gold_number_tv = (TextView) findViewById(R.id.gold_number_tv);
        show_number_tv = (TextView) findViewById(R.id.show_number_tv);
        one_quality_tv = (TextView) findViewById(R.id.one_quality_tv);
        year_tv = (TextView) findViewById(R.id.year_tv);
        quality_tv = (TextView) findViewById(R.id.quality_tv);
        gold_type_tv = (TextView) findViewById(R.id.gold_type_tv);

        ts = findViewById(R.id.ts);
        bank_user_name_tv = (TextView) findViewById(R.id.bank_user_name);
        bank_mame_tv = (TextView) findViewById(R.id.bank_mame);
        bank_card_number_tv = (TextView) findViewById(R.id.bank_card_number);
        bank_right_img = findViewById(R.id.bank_right_img);
    }

    private void initTirle() {
        title = (TextView) findViewById(R.id.view_main_middle_title);
        title.setText("卖出金币");
        findViewById(R.id.btn_main_left).setOnClickListener(this);
        title.setFocusable(true);
        title.setFocusableInTouchMode(true);
        title.requestFocus();
    }

    private HashMap<String,Object> map;
    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.btn_main_left:
                finish();
                break;
            case R.id.select_bank_card://选择银行卡

                //startActivity(new Intent(this,BankCardActivity.class).putExtra("type","选择银行卡"));
                startActivityForResult(new Intent(SaleGoldenActivity.this,BankCardActivity.class).putExtra("type","选择银行卡"),SELECT_BANK);
                break;
            case R.id.sale_number_lay:
                ArrayList<String> list = new ArrayList<>();
                for (int i = 1; i <= Integer.parseInt(myGoods.getShowNumber()); i++) {
                    list.add(i+"");
                }
                startActivityForResult(new Intent(this, SetBankActivity.class).putExtra("list", list), SET_BANK_REQUESTCODE);
                overridePendingTransition(R.anim.push_up_in,R.anim.push_up_out);
                break;
            case R.id.btn:
                if (num < 1){
                    MyToast("请选择商品卖出数量");
                }else if (id == null || "".equals(id)){
                    MyToast("请选择银行卡");
                }else {
                    map = new HashMap<>();
                    map.put("goodNumber",num+"");
                    map.put("userBankId",id);
                    map.put("goodId",myGoods.getGoodId());
                    map.put("userId",MyApplication.getInstance().userBean.getUserId());
                    //map.put("userId","158");
                    Intent intent = new Intent(SaleGoldenActivity.this, InPutPayPassWordActivity.class);
                    intent.putExtra("type",2);
                    startActivityForResult(intent,PAY_PASSWORK);
                    overridePendingTransition(R.anim.push_up_in,R.anim.push_up_out);
                }


                break;
        }
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
       if (requestCode == SET_BANK_REQUESTCODE){
            if (resultCode == RESULT_OK){
                num = Integer.parseInt(data.getStringExtra("bank_name"));

                sale_number_tv.setText(data.getStringExtra("bank_name"));
                DecimalFormat df = new DecimalFormat("##.##");
                double all =num * Double.parseDouble(myGoods.getGoodPrice());
                sale_all_money_tv.setText(df.format(all)+"元");
                double consume = all * rate ;
                consume_tv.setText(df.format(all-consume)+"元");
                account_money_tv.setText(df.format(consume)+"元");
               /* bank_name = data.getStringExtra("bank_name");
                bank_mame_tv.setText(bank_name);*/
            }
        }else if (requestCode == PAY_PASSWORK){
           if (resultCode == RESULT_OK){
               String pay_passwork = data.getStringExtra("passwork");
               Log.e(TAG, "onActivityResult: "+pay_passwork );
               map.put("payPass",pay_passwork);
               sell_coin(map);
           }
       }else if (requestCode == SELECT_BANK){
           if (resultCode == RESULT_OK){
               bank_mame = data.getStringExtra("bank_mame");
               bank_card_number = data.getStringExtra("bank_card_number");
               id = data.getStringExtra("id");
               name = data.getStringExtra("name");
               String branch= data.getStringExtra("branch");
               bank_user_name_tv.setText(name);
               bank_mame_tv.setText(bank_mame+"   "+branch);
               bank_card_number_tv.setText(bank_card_number);
               isshowbank();
           }
       }
    }

    private void isshowbank() {
        if (id == null || "".equals(id)){
            bank_right_img.setVisibility(View.VISIBLE);
            ts.setVisibility(View.VISIBLE);
        }else {
            bank_right_img.setVisibility(View.GONE);
            ts.setVisibility(View.GONE);
        }
    }


    /**
     * 更改字体颜色
     * @param color
     * @return
     */
    public  SpannableString setColotSpannableString(int color,String s){
        SpannableString member = new SpannableString(s);
        member.setSpan(new ForegroundColorSpan(color), s.length()-myGoods.getVariation().length()-3, s.length()-2, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        return member;
    }

    private void sell_coin(Map<String,Object> map){

        showDialog();
        XUtil.Post(ConfigUtil.SELL_COIN,map,new MyCallBack<String>(){
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                Log.i(TAG, "onSuccess: "+result);
                try {
                    JSONObject j = new JSONObject(result);
                    String re = j.optString("result");
                    String info = j.optString("info");
                    MyToast(info);
                    if ("1".equals(re)){
                        MyApplication.getInstance().upUser();
                        finish();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFinished() {
                super.onFinished();
                closeDialog();
            }
        });
    }


}
